package com.example.tianxiaoyu.zyfypt__128txy.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.tianxiaoyu.zyfypt__128txy.activity.ViewArticleActivity;
import com.example.tianxiaoyu.zyfypt__128txy.activity.ViewTwareActivity;
import com.example.tianxiaoyu.zyfypt__128txy.bean.ArticleBean;
import com.example.tianxiaoyu.zyfypt__128txy.R;
import com.example.tianxiaoyu.zyfypt__128txy.bean.ProjectBean;
import com.squareup.picasso.Picasso;

import java.util.List;


public class ArticleAdapter extends RecyclerView.Adapter <ArticleAdapter.ViewHolder>{
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<ArticleBean> list;
    //构造方法，传进上下文
    public ArticleAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(this.context);
    }
    //自定义 ：传进数值
    public void setList(List<ArticleBean> list) {
        this.list=list;
    }


    //
    public class ViewHolder extends RecyclerView.ViewHolder
    {
        ImageView imageView;
        TextView tvtitle,tvdescript,tvtime;
        Button button;


        public ViewHolder(View itemView)
        {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            tvdescript=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
            button=(Button)itemView.findViewById(R.id.button);
        }
    }
    //为了
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ArticleBean articleBean = list.get(position);
        if (articleBean == null)

            return;
       // holder.imageView.setImageResource(articleBean.getArticleId());
        holder.tvtitle.setText(articleBean.getName());
        holder.tvdescript.setText(articleBean.getDescription());
        holder.tvtime.setText(articleBean.getUpdate_time());
        //异步加载图片
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"
                +articleBean.getThumb() )
                .placeholder(R.mipmap.ic_launcher)
                .into(holder.imageView);
        //
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);//实际应该操作网络/数据库
                notifyDataSetChanged();
            }
        });
        //
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目
                int id=list.get(position).getId();
                Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(context, ViewArticleActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                intent.putExtra("userid",list.get(position).getUserid());
                context.startActivity(intent);
            }
        });
    }
    //公有多少个条目
    @Override
    public int getItemCount() {
        if (list==null) return 0;
        return list.size();
    }
}
